package view;

import org.apache.ibatis.session.SqlSession;
import orm.MyBatisSQLSession;
import orm.Word;
import orm.WordMapper;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.util.Arrays;
import java.util.Collections;

public class MainViewAction {
    public static void refreshWords() {
        refreshWords(false);
    }

    public static void refreshWords(boolean random) {
        WordMapper wordMapper = MyBatisSQLSession.getWordMapper();
        String text = "%" + MainView.searchField.getText() + "%";
        Word[] words = wordMapper.search(
                text,
                MainView.searchFavoriteCheckBox.isSelected(),
                MainView.userId
        );
        if (random) {
            Collections.shuffle(Arrays.asList(words));
        }
        MainView.wordsContainer.removeAll();
        for (Word word : words) {
            WordItem wordItem = new WordItem(
                    word.getId(),
                    word.getWord(),
                    word.getMeaning(),
                    word.getSentence(),
                    word.getAudio(),
                    word.isFavorite()
            );
            MainView.wordsContainer.add(wordItem);
        }
        MainView.scrollPanel.setViewportView(MainView.wordsContainer);
    }

    static void addListener() {
        MainView.searchAllButton.addActionListener(actionEvent -> {
            refreshWords();
        });

        MainView.randomButton.addActionListener(actionEvent -> {
            refreshWords(true);
        });

        MainView.addingButton.addActionListener(actionEvent -> {
            String word = MainView.wordTextField.getText();
            String meaning = MainView.meaningTextField.getText();
            if (word.isEmpty() || meaning.isEmpty()) {
                showHint("英文单词或中文释义为空");
                return;
            }
            Word word1 = new Word(null,
                    word,
                    meaning,
                    MainView.sentenceTextField.getText(),
                    MainView.audioTextField.getText(),
                    true, // 默认收藏新添加单词
                    MainView.userId
            );
            SqlSession sqlSession = MyBatisSQLSession.getSqlSession();
            WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
            wordMapper.insert(word1);
            showHint("已添加 " + word);
            refreshWords();
        });

        MainView.searchField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent documentEvent) {
                refreshWords();
            }

            @Override
            public void removeUpdate(DocumentEvent documentEvent) {
                refreshWords();
            }

            @Override
            public void changedUpdate(DocumentEvent documentEvent) {
                refreshWords();

            }
        });

        MainView.searchFavoriteCheckBox.addItemListener(itemEvent -> {
            refreshWords();
        });
    }

    public static void showHint(String hint) {
        class Sleep implements Runnable {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MainView.hintLabel.setText("");
            }
        }
        MainView.hintLabel.setText(hint);
        new Thread(new Sleep()).start();
    }

}
